/*进入页面刷新数据*/
getAllPlace();


/*修改密码字段验证*/
$("#changePasswordModal").bootstrapValidator({
	message:"这是无效的数据域",
	feedbackIcons: {/*input状态样式图片*/
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields:{
    	changePassword: {
            message:'密码无效',
            validators: {
                notEmpty: {
                    message: '密码不能为空'
                },
                stringLength: {
                    min: 6,
                    max: 30,
                    message: '用户名长度必须在6到30之间'
                },
                identical: {//相同
                    field: 'changePassword', //需要进行比较的input name值
                    message: '两次密码不一致'
                },
            }
        },
        repChangePassword: {
            message: '密码无效',
            validators: {
                notEmpty: {
                    message: '密码不能为空'
                },
                stringLength: {
                    min: 6,
                    max: 30,
                    message: '密码长度必须在6到30之间'
                },
                identical: {//相同
                    field: 'changePassword',
                    message: '两次密码不一致'
                },
            }
        },
    }
});

/*修改密码*/
$("#changPasswordBtn").click(function(){
	$.ajax({
		url:"changePassword",
		data:{
			oldPassowrd:$("#oldPassword").val(),
			password:$("#changePassword").val(),
		},
		type:"POST",
		success:function(result){
			if(result.resultParm.code == "200"){
				alert(result.resultInfo);
			} else {
				alert(result.resultInfo);
				$("#changePasswordModal").modal("hide");
				alert("请重新登录!");
				window.location.href="index.html";
			}
		},
		error:function(){
			alert("修改密码失败");
		}
	});
});
/*获取所有场地信息*/
function getAllPlace(){
	$.ajax({
		type:"get",
		url:"findAllPlaces",
		success:function(result){
			console.log(result);
			for(var i=0;i<result.resultParm.places.length;i++){
				var newLine = 
					"<tr>"+
						"<td>"+result.resultParm.places[i].placeId+"</td>"+
						"<td>"+result.resultParm.places[i].placeName+"</td>"+
						"<td>"+result.resultParm.places[i].placeMoney+"</td>"+
						"<td>"+"<a title="+"\"changeA\""+ "data-toggle="+"\"modal\""+" href="+"\'#change-place-modal\'"+">修改</a>"+"</td>"+
						"<td>"+"<a title="+"\"deleteA\""+ "href="+"#"+">删除</a>"+"</td>"
					"</tr>";
				$("table").append(newLine);
			};
			$("a[title=changeA]").click(function(){
				$("#change-placeId").val($(this).parent().parent().children().eq(0).text());
				$("#change-placeName").val($(this).parent().parent().children().eq(1).text());
				$("#change-Money").val($(this).parent().parent().children().eq(2).text());
			});
			deleteA();
		},
	});
};
/*确认修改按钮*/
$("#change").click(function(){
		var r=confirm("确认修改么？");
		if(r==true){
			$.ajax({
				type:"post",
				url:"updataPlace",
				data:{
					placeId:$("#change-placeId").val(),
					placeName:$("#change-placeName").val(),
					placeMoney:$("#change-Money").val(),
				},
				success:function(result){
					alert(result.resultInfo);
					$("#change-place-modal").modal("hide");
					$(".personal-game table tr td").remove();
					getAllPlace();
				},
				error:function(){
					alert("修改失败");
				}
			});
		};
});

/*删除超链接*/
function deleteA(){
	$("a[title=deleteA]").click(function(){
		var r=confirm("确认删除么？");
		if(r==true){
			$.ajax({
				type:"get",
				url:"deletePlaceHandler",
				data:{
					placeId:$(this).parent().parent().children().eq(0).text(),
				},
				success:function(result){
					alert(result.resultInfo);
					$("table tr td").remove();
					getAllPlace();
				},
				error:function(){
					alert("删除失败");
				}
			});
		};
	});
};
/*增加场地按钮*/
$("#add").click(function(){
	var r=confirm("确认增加么？");
	if(r==true){
		$.ajax({
			type:"post",
			url:"addPlaceHandler",
			data:{
				placeName:$("#placeName").val(),
				placeMoney:$("#placeMoney").val(),
			},
			success:function(result){
				alert(result.resultInfo);
				$("#add-place-modal").modal("hide");
				$("table tr td").remove();
				getAllPlace();
			},
			error:function(){
				alert("添加失败");
			}
		});
	};
});
/*查询按钮*/
$("#search-place").click(function(){
	$.ajax({
		type:"post",
		url:"findAllByName",
		data:{
			name:$("#equipmentName").val(),
		},
		success:function(result){
			$("table tr td").remove();
			var newLine = 
					"<tr>"+
						"<td>"+result.resultParm.places[0].placeId+"</td>"+
						"<td>"+result.resultParm.places[0].placeName+"</td>"+
						"<td>"+result.resultParm.places[0].placeMoney+"</td>"+
						"<td>"+"<a title="+"\"changeA\""+ "data-toggle="+"\"modal\""+" href="+"\'#change-place-modal\'"+">修改</a>"+"</td>"+
						"<td>"+"<a title="+"\"deleteA\""+ "href="+"#"+">删除</a>"+"</td>"
					"</tr>";
			$("table").append(newLine);
			changeBtn();
			deleteA();
		},
	});
});